package com.test.blackwhite.slice;

import com.test.blackwhite.DoubleGameAbility;
import com.test.blackwhite.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.bundle.AbilityInfo;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;
import ohos.rpc.RemoteException;

import java.util.List;

public class SelectDoubleGameAbilitySlice extends AbilitySlice {
    private static String A = "a";
    private static String B = "b";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_selectdoubelgame);

        clickaction();
    }
    public void clickaction() {
        findComponentById(ResourceTable.Id_button_threegame).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getDifficulty(3);
            }
        });

        findComponentById(ResourceTable.Id_button_fourgame).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getDifficulty(4);
            }
        });

        findComponentById(ResourceTable.Id_button_fivegame).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getDifficulty(5);
            }
        });

        findComponentById(ResourceTable.Id_button_sixgame).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getDifficulty(6);
            }
        });

        findComponentById(ResourceTable.Id_button_sevengame).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getDifficulty(7);
            }
        });

        findComponentById(ResourceTable.Id_button_eightgame).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getDifficulty(8);
            }
        });

        findComponentById(ResourceTable.Id_button_ninegame).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getDifficulty(9);
            }
        });

        findComponentById(ResourceTable.Id_button_tengame).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getDifficulty(10);
            }
        });

        findComponentById(ResourceTable.Id_button_doubleback).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
    }

    private void getDifficulty(int difficulty){
        Intent intent = new Intent();
        intent.setParam("difficulty", difficulty);
        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        for (DeviceInfo deviceInfo : deviceInfoList) {
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId(deviceInfo.getDeviceId())
                    .withBundleName(getBundleName())
                    .withAbilityName(DoubleGameAbility.class.getName())
                    .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                    .build();
            intent.setOperation(operation);
            try {
                List<AbilityInfo> abilityInfoList = getBundleManager().queryAbilityByIntent(intent,0,0);
                if (abilityInfoList != null && !abilityInfoList.isEmpty()) {
                    intent.setParam("A",B);
                    startAbility(intent);
                }
            } catch (RemoteException remoteException) {

            }
        }
        intent.setParam("A",A);
        present(new DoubleGameAbilitySlice(),intent);
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
